-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cast numpy
scalars to arrays in as_compatible_data
#9403
Conversation
I think the equivalent check is here xarray/xarray/namedarray/core.py Line 207 in 19a0428
This does a runtime_checkable check against the |
|
Returning scalars is not supposed to happen if the array api is respected. Hopefully numpy considers this a a bug too. import numpy as np
import array_api_strict as xps
a = xps.asarray([1,2.0])
xps.mean(a)
Out[21]: Array(1.5, dtype=array_api_strict.float64)
a = np.asarray([1,2.0])
np.mean(a)
Out[23]: np.float64(1.5) |
Indeed. However, that is only if you read it as "only arrays can be array API compliant". It appears that |
It surprised me that this is even allowed within the array API. For example In [1]: import numpy as np
In [2]: s = np.float64(4.1)
In [3]: result = np.broadcast_to(s, (2, 2))
In [4]: result
Out[4]:
array([[4.1, 4.1],
[4.1, 4.1]])
In [5]: type(result)
Out[5]: numpy.ndarray
In [6]: type(s)
Out[6]: numpy.float64 means that in the I wonder if there are other places in xarray where we assume that for an array-API-compliant duck type, the type going into an array API function/method is going to be the same as the type coming out (because apparently it doesn't have to be). |
@Illviljan, should I try to apply something like this to Otherwise I believe this should be ready for a final review? |
@@ -311,7 +311,7 @@ def as_compatible_data( | |||
else: | |||
data = np.asarray(data) | |||
|
|||
if not isinstance(data, np.ndarray) and ( | |||
if not isinstance(data, np.ndarray | np.generic) and ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is worth adding a comment, noting that we want to cast numpy scalars to arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the latest commit does that, but please check if that's clear enough
(since the only thing left to review is the comment – we've got one approval already – I'll merge this tomorrow) |
merging since this breaks compat with |
* also call `np.asarray` on numpy scalars * check that numpy scalars are properly casted to arrays * don't allow `numpy.ndarray` subclasses * comment on the purpose of the explicit isinstance and `np.asarray`
Variable
may contain numpy scalars withnumpy>=2.1
#9399, closes numpy2.1 + strings caussing errors in assignment #9535, closes Fix numpy 2.1 strings #9536As mentioned in #9399,
numpy
recently added__array_namespace__
to scalars, which will causeVariable
to wrap the scalar. This also castsnumpy
scalars tonumpy.ndarray
if it ever reachesas_compatible_data
.This will most likely also pop up in
NamedArray
/xarray.namedarray.from_array
, so we might have to figure out what to do there. cc @andersy005 and @Illviljan for awareness